{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "89db887c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = {}\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dfc61711",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 1, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['e'] = found['e']+1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a27b8554",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 2, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['e']+=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2df92e21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "e\n",
      "i\n",
      "o\n",
      "u\n"
     ]
    }
   ],
   "source": [
    "for k in found:\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "62a0081c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k in found:\n",
    "    print(k,\"was found\",found[k],\"time(s).\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "573ab3ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k in sorted(found):\n",
    "    print(k,\"was found\",found[k],\"time(s).\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3aceb845",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k,v in sorted(found.items()):\n",
    "    print(k,\"was found\",v,\"time(s).\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c337269d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels: hitch-hiker\n",
      "a was found 0 time(s).\n",
      "e was found 1 time(s).\n",
      "i was found 2 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"provide a word to search for vowels: \")\n",
    "found = {}\n",
    "found[\"a\"] = 0\n",
    "found[\"e\"] = 0\n",
    "found[\"i\"] = 0\n",
    "found[\"o\"] = 0\n",
    "found[\"u\"] = 0\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] +=1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,\"was found\",v,\"time(s).\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e3caedf9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'banana': 2, 'peare': 2}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruites = {}\n",
    "fruites[\"apples\"] = 10\n",
    "fruites\n",
    "if \"banana\" in fruites:\n",
    "    fruites[\"banana\"]+=1\n",
    "else:\n",
    "    fruites[\"banana\"] = 1\n",
    "fruites\n",
    "if \"banana\" in fruites:\n",
    "    fruites[\"banana\"]+=1\n",
    "else:\n",
    "    fruites[\"banana\"] = 1\n",
    "fruites\n",
    "if \"peare\" not in fruites:\n",
    "    fruites[\"peare\"] = 0\n",
    "fruites[\"peare\"] += 1\n",
    "fruites\n",
    "fruites.setdefault(\"peare\",0)\n",
    "fruites[\"peare\"] +=1\n",
    "fruites"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2613ec8",
   "metadata": {},
   "source": [
    "### 对比一下setdefault（帮助避免keyerror异常）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "0759e4d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels: hitch-hiker\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'i'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [27]\u001b[0m, in \u001b[0;36m<cell line: 4>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m letter \u001b[38;5;129;01min\u001b[39;00m word:\n\u001b[0;32m      5\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m letter \u001b[38;5;129;01min\u001b[39;00m vowels:\n\u001b[1;32m----> 6\u001b[0m         found[letter] \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m\n\u001b[0;32m      7\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k,v \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28msorted\u001b[39m(found\u001b[38;5;241m.\u001b[39mitems()):\n\u001b[0;32m      8\u001b[0m     \u001b[38;5;28mprint\u001b[39m(k,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwas found\u001b[39m\u001b[38;5;124m\"\u001b[39m,v,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtime(s).\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mKeyError\u001b[0m: 'i'"
     ]
    }
   ],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"provide a word to search for vowels: \")\n",
    "found = {}\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] +=1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,\"was found\",v,\"time(s).\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a7c1c3da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels: hitch-hiker\n",
      "e was found 1 time(s).\n",
      "i was found 2 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"provide a word to search for vowels: \")\n",
    "found = {}\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)\n",
    "        found[letter] +=1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,\"was found\",v,\"time(s).\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd29e8ab",
   "metadata": {},
   "source": [
    "## set快速生成集合   union或“|\"合并集合(并集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1e20f069",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'h', 'i', 'l', 'o', 'u']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set(\"aeiou\")\n",
    "word = \"hello\"\n",
    "u = vowels.union(set(word))\n",
    "u_list = list(sorted(list(u)))\n",
    "u_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "87cf928a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'h', 'i', 'l', 'o', 'u']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set(\"aeiou\")\n",
    "word = \"hello\"\n",
    "u = vowels|(set(word))\n",
    "u_list = list(sorted(list(u)))\n",
    "u_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cee50c21",
   "metadata": {},
   "source": [
    "## difference找不是共有的元素（差集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7e611b06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = vowels.difference(set(word))\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2acc17f9",
   "metadata": {},
   "source": [
    "## intersection或\"&\"找共同对象（交集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5ea332da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = vowels.intersection(set(word))\n",
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "960f2581",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = vowels&(set(word))\n",
    "i"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77f918b8",
   "metadata": {},
   "source": [
    "## p129"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "044c386b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels: hitch-hiker\n",
      "i\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"provide a word to search for vowels: \")\n",
    "vowels = set(vowels)\n",
    "found = vowels&set(word)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "065e75dd",
   "metadata": {},
   "source": [
    "## P139嵌套字典     pprint美观打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "bd2d20b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'1': {'姓名': '小明', '性别': '男'},\n",
      " '2': {'姓名': '小黄', '性别': '女'},\n",
      " '3': {'姓名': '小白', '性别': '男'}}\n"
     ]
    }
   ],
   "source": [
    "people = {}\n",
    "people[\"1\"] = {\"姓名\":\"小明\",\n",
    "              \"性别\":\"男\",\n",
    "              }\n",
    "people[\"2\"] = {\"姓名\":\"小黄\",\n",
    "              \"性别\":\"女\",\n",
    "              }\n",
    "people[\"3\"] = {\"姓名\":\"小白\",\n",
    "              \"性别\":\"男\",\n",
    "              }\n",
    "import pprint\n",
    "pprint.pprint(people)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6c365efb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'姓名': '小黄', '性别': '女'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people['2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9f8273be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'女'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people['2'][\"性别\"]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
